package demo.main;

import static net.sf.dynamicreports.report.builder.DynamicReports.col;
import static net.sf.dynamicreports.report.builder.DynamicReports.grid;
import static net.sf.dynamicreports.report.builder.DynamicReports.report;
import static net.sf.dynamicreports.report.builder.DynamicReports.stl;
import static net.sf.dynamicreports.report.builder.DynamicReports.type;

import java.math.BigDecimal;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.dynamicreports.report.builder.column.TextColumnBuilder;
import net.sf.dynamicreports.report.builder.style.StyleBuilder;
import net.sf.dynamicreports.report.exception.DRException;
import net.sf.jasperreports.engine.JRDataSource;

import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;

public class Crosstab extends MutiReport {
	@At("/getCrosstab")
	@Ok("json")
	public String getCrosstab(HttpServletRequest request, HttpServletResponse response) {
		System.out.println("getCrosstab");
		build(request, response);
		return resultResult;
	}

	@Override
	protected void build(HttpServletRequest request, HttpServletResponse response) {
		StyleBuilder textStyle = stl.style(Templates.columnStyle).setBorder(
				stl.pen1Point());

		TextColumnBuilder<String> itemColumn = col.column("产品", "item",
				type.stringType());
		TextColumnBuilder<Integer> quantityColumn = col.column("数量",
				"quantity", type.integerType());
		TextColumnBuilder<BigDecimal> unitPriceColumn = col.column(
				"单价", "unitprice", type.bigDecimalType());
		TextColumnBuilder<Date> orderDateColumn = col.column("日期",
				"orderdate", type.dateType());
		TextColumnBuilder<Date> orderDateFColumn = col.column("日期",
				"orderdate", type.dateYearToFractionType());
		TextColumnBuilder<Date> orderYearColumn = col.column("年",
				"orderdate", type.dateYearType());
		TextColumnBuilder<Date> orderMonthColumn = col.column("月",
				"orderdate", type.dateMonthType());
		TextColumnBuilder<Date> orderDayColumn = col.column("日",
				"orderdate", type.dateDayType());

		try {
			reportBuilder = report().setTemplate(Templates.reportTemplate)
					.setColumnStyle(textStyle)
					.columns(itemColumn, quantityColumn, unitPriceColumn,
							orderDateColumn, orderDateFColumn, orderYearColumn,
							orderMonthColumn, orderDayColumn)
					.columnGrid(
							grid.verticalColumnGridList(itemColumn, grid
									.horizontalColumnGridList(quantityColumn,
											unitPriceColumn)),
							grid.verticalColumnGridList(orderDateColumn, grid
									.horizontalColumnGridList(orderDateFColumn,
											orderYearColumn), grid
									.horizontalColumnGridList(orderMonthColumn,
											orderDayColumn)))
					.title(Templates.createTitleComponent(""))
					.pageFooter(Templates.footerComponent)
					.setDataSource(createDataSource());
			setResult(reportBuilder.toJasperPrint(), request, response);
		} catch (DRException e) {
			e.printStackTrace();
		}
	}

	private JRDataSource createDataSource() {
		DataSource dataSource = new DataSource("item", "orderdate", "quantity",
				"unitprice");
		dataSource.add("笔记本", new Date(), 1, new BigDecimal(500));
		dataSource.add("图书", new Date(), 7, new BigDecimal(300));
		dataSource.add("PDA", new Date(), 2, new BigDecimal(250));
		return dataSource;
	}

}
